<?php 
/**
 *猜拳游戏
 */
class Module_mora{
	/**
	 * 构造函数
	 */
	function __construct() {
	
	}
	/**
	 * 猜拳首页
	 * 剪刀（shears）
	 * 石头（rock）
	 * 布（cloth）
	 */
	function index(){
		$uin = sessionInfo('uin');
		$smarty = loadSmarty();
		
		loadLib('App_mora');
		//筹码余额
		loadLib('Currency');
		$stake = Currency::get('app_21' , $uin);
		$smarty->assign('stake' , $stake);
		// 获取自己的战绩
		$gains = App_mora::getGain($uin , false);
		$newbie = false;
		if(empty($gains)) {
			$gains = App_mora::getGain($uin , true);
			$newbie = true;
		}
		// 获取猜拳称号
		$rank = App_mora::getRank($uin , $gains['gain']);
		//print_r($rank);
			
		// 如果是第一次来，则奖励10000筹码
		if($newbie) {
			loadLib('Currency');
			Currency::update('app_21' , $uin , 10000);
		}
		//获取是否存在该局
		$result = App_mora::getRandStateList();
		//未完成拳局
		$undone = App_mora::getRandUndoneList($uin);
		$list = array();
		foreach($result as $k => $row) {
			if($row['uin'] == $uin) continue;	// 不显示自己的
			
			$list[$k] = $row;
		}
		//print_r($list);
		$uin_list = array();
		foreach($list as $row) {
			$uin_list[] = $row['uin'];
			$gains[$row['uin']] = $row['gain'];
		}
		
		if(!empty($undone)){
			$uin_undone = array();
			foreach($undone as $row) {
				$uin_undone[] = $row['uin'];
				$gain[$row['uin']] = $row['gain'];
			}
		}
		loadLib('User');
		
		//调用战报
		loadLib('App_battlefie');
		$gains_list = App_battlefie::getBattlefie();
		
		$smarty->assign('gains_list' , $gains_list);

		// 获取用户等级信息
		$gains_info = App_mora::getRanks($uin_list , $gains);
		//未完成的局
		if(!empty($undone)){
			$gain_info = App_mora::getRanks($uin_undone , $gain);
		}
		if(!empty($uin_list)) {
			
			$uin_info = User::getBaseInfo($uin_list);
			
			foreach($list as $k => $row) {
				$list_uin = $row['uin'];
				$list[$k]['uin_name'] = isset($uin_info[$row['uin']]) ? $uin_info[$row['uin']]['name'] : '';
				$list[$k]['uin_sex'] = isset($uin_info[$row['uin']]) ? $uin_info[$row['uin']]['sex'] : '';
				$list[$k]['rank'] = $gains_info[$list_uin];
			}
		}
		//未完成局的信息
		if(!empty($uin_undone)) {		
			$undone_info = User::getBaseInfo($uin_undone);
			foreach($undone as $k => $row) {
				$undone_uin = $row['uin'];
				$undone[$k]['uin_name'] = isset($undone_info[$row['uin']]) ? $undone_info[$row['uin']]['name'] : '';
				$undone[$k]['uin_sex'] = isset($undone_info[$row['uin']]) ? $undone_info[$row['uin']]['sex'] : '';
				$undone[$k]['rank'] = $gain_info[$undone_uin];
			}
		}
		//print_r($undone);
		$smarty->assign('list' , $list);
		$smarty->assign('undone' , $undone);
		$smarty->assign('rank' , $rank);
		$smarty->assign('newbie' , $newbie);
		
		return $smarty->fetch('app/mora/index.tpl.php');
	}
	/**
	 * 出拳页面
	 */
	function punch(){
		$uin = sessionInfo('uin');
		$smarty = loadSmarty();
		loadLib('Currency');
		
		$currency = Currency::get('app_21', $uin , 'currency');
		
		// 判断钱是否够，不够就提示去用金币换，或者退出
		if($currency < 50) {			
			return View::error('一次最少下注50筹码，你现在只有' . $currency . '筹码,去黑市兑换');
		}
		
		return $smarty->fetch('app/mora/punch.tpl.php');
	}
	/**
	 * 开始出拳
	 */
	function open(){
		$uin = sessionInfo('uin');
		$out = isset($_POST['out']) ? trim($_POST['out']) : null;
		$smarty = loadSmarty();
		
		loadLib('App_mora');
		
		$mora = App_mora::getMora($uin);
		//print_r($mora);
		
		if(!empty($mora['id'])){
			$id=$mora['id'];
			if(empty($mora['state'][2])){
				App_mora::getUpMora($id , $uin , $out ,array(),$mora);
				$mora = App_mora::getMora($uin);
			
				$count=count($mora['state']);
				//当猜拳满3次自动跳转
				if($count == 3){
					header('Location: ' . SITEURL . '/app.php?m=mora&a=end&uin=' . sessionInfo('uin') . '&id=' . $id . '&sid=' . sessionInfo('skey') );
				}
			}
		}else{
			loadLib('Currency');
			
			$currency = Currency::get('app_21', $uin , 'currency');
			
			// 判断钱是否够，不够就提示去用金币换，或者退出
			if($currency < 50) {			
				return View::error('一次最少下注50筹码，你现在只有' . $currency . '筹码,去黑市兑换');
			}
			Currency::update('app_21' , $uin , -50);
			
			$id = 0;
			$code = App_mora::recordState($uin , $id , 0 , 0 , $out , 50);
			loadLib('Operate');
			Operate::getAdd($uin , 0 , 'app_mora');
			if($code !== 0) {
				return View::error('系统繁忙，请稍后再试' , $code);
			}
			$mora = App_mora::getMora($uin);
			
		}
		$smarty->assign('mora',$mora);
		//print_r($mora);
		
		return $smarty->fetch('app/mora/open.tpl.php');
	}
	/**
	 * 获取排行榜信息
	 */
	function top(){
		
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$top_type = isset($_GET['type']) && in_array($_GET['type'] , array('rate' , 'gain')) ? trim($_GET['type']) : 'gain';
		
		$smarty = loadSmarty();
		// 获取排行
		loadLib('App_mora');
		//获取排行总量
		$mora_count = App_mora::getTopCount();
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		
		$list = App_mora::getTopList( $top_type ,$page,$per_page);
		//print_r($list);
		$uin_arr = array();
		$gains = array();
		foreach($list as $row) {
			$uin_arr[] = $row['uin'];
			$gains[$row['uin']] = $row['gain'];
		}
		// 获取用户等级信息
		$gains_info = App_mora::getRanks($uin_arr , $gains);
		
		loadLib('User');
		$uin_arr_info = User::getBaseInfo($uin_arr);
		foreach($list as $k => $row) {
			$list_uin = $row['uin'];
			$list[$k]['name'] = isset($uin_arr_info[$list_uin]) ? $uin_arr_info[$list_uin]['name'] : '';
			$list[$k]['sex'] = isset($uin_arr_info[$list_uin]) ? $uin_arr_info[$list_uin]['sex'] : '';
			$list[$k]['rank'] = $gains_info[$list_uin];
			$list[$k]['rate'] = sprintf('%.2f' , $row['rate'] / 100);
		}
		//print_r($list);
		$smarty->assign('list' , $list);
		$smarty->assign('top_type' , $top_type);
		return $smarty->fetch('app/mora/top.tpl.php');
	}

	/**
	 * 进入别人拳局
	 */
	function challenge(){
		$owner_uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		
		// 异常判断
		if(empty($owner_uin) || $id <= 0) {
			return View::error('系统繁忙，请稍后再试');
		}
		$uin = sessionInfo('uin');
		
		$is_own = $uin == $owner_uin ? true : false;
		
		if($is_own) {
			return View::error('不能应战自己的赌局');
		}
		// 获取是否存在该局
		loadLib('App_mora');
		
		$state = App_mora::getState($owner_uin , $id);
	//print_r($state);
		if(empty($state)) {
			return View::error('该局已结束');
		}
	
		if($state['guest_uin'] > 0 || $state['turn'] != 1) {
			return View::error('你下手太慢拉，别人已经应战了');
		}
		$judge = App_mora::getList($uin , 1 , 1);
		//print_r($judge);
		if(empty($judge)) {
			return View::error('你要自己开一局后才能应战别人,现在去开一局');
		}
		loadLib('Currency');
			
		$currency = Currency::get('app_21', $uin , 'currency');
			
		// 判断钱是否够，不够就提示去用金币换，或者退出
		if($currency < $state['stake']) {			
			return View::error('你的筹码不够 ' . $state['stake'] . '，不能应战,去黑市兑换');
		}
		
		$stake = $state['stake']*2;
		//print_r($stake);
		Currency::update('app_21' , $uin , -$state['stake']);
		App_mora::addstake($owner_uin , $id , 2 , $stake);
		
		App_mora::getUpState($id , $owner_uin , $uin);
		loadLib('Operate');
		Operate::getAdd($uin , $owner_uin , 'app_mora_challenge');
		
	//防止刷新
		header('Location: ' . SITEURL . '/app.php?m=mora&a=chall&uin=' . sessionInfo('uin') . '&owner_uin=' . $owner_uin . '&id=' . $id . '&sid=' . sessionInfo('skey') );
	}
	//跳转后的页面
	function chall(){
		$owner_uin = isset($_GET['owner_uin']) ? intval($_GET['owner_uin']) : null;
		
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		
		$smarty = loadSmarty();
		
		
		$smarty->assign('owner_uin' , $owner_uin);
		$smarty->assign('id' , $id);
		
		return $smarty->fetch('app/mora/challenge.tpl.php');
	}
	/**
	 * 结束拳局，不再出拳
	 */
	function end(){
		$owner_uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$smarty = loadSmarty();
		// 异常判断
		if(empty($owner_uin) || $id <= 0) {
			return View::error('系统繁忙，请稍后再试');
		}
		
		$uin = sessionInfo('uin');
		
		// 获取是否存在该局
		loadLib('App_mora');
		$state = App_mora::getState($owner_uin , $id);
		$stake = $state['stake'];
		if(empty($state['state'][2])){
				return View::error('需要出三次拳');
		}
		//print_r($state);
		if(empty($state)) {
			return View::error('该局已结束');
		}
		App_mora::addstake($uin , $id , 1 , 50);
		loadLib('Currency');
		$balance = Currency::get('app_21' , $owner_uin);
		$smarty->assign('balance',$balance);
		//print_r($state['state'][0]);
		$smarty->assign('state' , $state['state']);
		$smarty->assign('stake' , $stake);
		$smarty->assign('owner_uin' , $owner_uin);
		$smarty->assign('id' , $id);
		return $smarty->fetch('app/mora/addstake.tpl.php');
	}
	/**
	 * 加注
	 */
	function addstake(){
		$owner_uin = isset($_POST['uin']) ? intval($_POST['uin']) : null;
		$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
		$add_stake = isset($_POST['add_stake']) ? intval($_POST['add_stake']) : null;
		
		// 判断用户是否够钱
		if($add_stake <= 0) {
			header('Location: ' . SITEURL . '/app.php?m=mora&a=endturn&uin='.$owner_uin.'&sid=' . sessionInfo('skey') );
		}
		loadLib('App_mora');
		loadLib('Currency');
		
		$state = App_mora::getState($owner_uin , $id);
		
		$currency = Currency::get('app_21', $owner_uin , 'currency');
			
		// 判断钱是否够，不够就提示去用金币换，或者退出
		if($currency < $add_stake) {			
			return View::error('你的筹码不够 ' . $add_stake . '，不能加注,去黑市兑换');
		}
		$code=Currency::update('app_21' , $owner_uin , -$add_stake);
	//	Money::update($owner_uin , -$add_stake);
		if($code === 0){
			loadLib('App_mora');
			$stake = $state['stake'] + $add_stake;
			App_mora::addstake($owner_uin , $id , 1 , $stake);
			//print_r($code);
				return View::success('加注 ' . $add_stake . ' 筹码成功');
			
		}else{
			return View::error('操作失败' , $code);
		}
	}
	/**
	 * 查看自己的拳局
	 */
	function endturn(){
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		
		$uin = sessionInfo('uin');
		loadLib('App_mora');
		$mora_count = App_mora::getCount($uin);
		
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		//计算总页数
		$mora_list = App_mora::getList($uin , $page , $per_page);
		
		//print_r($mora_list);
		$smarty = loadSmarty();
		
		$smarty->assign('mora' , $mora_list);
		$smarty->assign('per_page' , $per_page);
		$smarty->assign('mora_count' , $mora_count);
		return $smarty->fetch('app/mora/myopen.tpl.php');
	}
	/**
	 * 应战拳局
	 * Enter description here ...
	 */
	function addopen(){
		$owner_uin = isset($_POST['owner_uin']) ? intval($_POST['owner_uin']) : null;
		$out = isset($_POST['out']) ? trim($_POST['out']) : null;
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		//进入应战，修改状态
		$smarty = loadSmarty();
		
		loadLib('App_mora');
		$uin = sessionInfo('uin');
		$mora = App_mora::getOneMora($owner_uin , $id);
		$state = App_mora::getState($owner_uin , $id);
		if(empty($state)) {
			return View::error('该局已结束');
		}
		if(empty($mora['state'][5])){
			App_mora::getUpMora($id , $owner_uin , array() , $out , $mora);
			$mora = App_mora::getOneMora($owner_uin , $id);		
		}
		$count = count($mora['state']);
		
		//当对战时猜拳满3次自动跳转
		if($count == 6){
			header('Location: ' . SITEURL . '/app.php?m=mora&a=endguest&uin=' . $owner_uin . '&id=' . $id . '&sid=' . sessionInfo('skey') );
		}
		//print_r($count);
		$k = $count - 1;
		
		$new = array();
		for($i = 3;$i <= $k;$i++){
			$new[] = $mora['state'][$i];
		}
		/***********************道具列表**********************/
		loadLib('App_prop');
		$oneselfprop = App_prop::getAllOneselfProp('app_mora_oneself_prop', 'app_mora_prop', $uin);
		foreach ($oneselfprop as $k => $row){
			if ($row['type'] == 'repeat'){
				$list[] = $row;
			}
		}
		
		if (!empty($list)){
			$smarty->assign('list',$list);
		}
		//print_r($list);
		$smarty->assign('new' , $new);
		$smarty->assign('uin' , $owner_uin);
		$smarty->assign('id' , $id);
		return $smarty->fetch('app/mora/upstate.tpl.php');
	}
	/**
	 * 玩家结束应战，不再出拳
	 * 剪刀（shears）
	 * 石头（rock）
	 * 布（cloth）
	 */
	function endguest(){
		$owner_uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$smarty = loadSmarty();
		// 异常判断
		if(empty($owner_uin) || $id <= 0) {
			return View::error('系统繁忙，请稍后再试');
		}
		
		$uin = sessionInfo('uin');
		
		// 获取是否存在该局
		loadLib('App_mora');
		$state = App_mora::getState($owner_uin , $id);
		if(empty($state)) {
			return View::error('该局已结束');
		}
		
		if(empty($state['state'][5])){
			return View::error('需要出三次拳');
		}
		
		$k = count($state['state'])-1;
		
		$owner = array();
		$guest = array();
		for($i = 0;$i <= $k-3;$i++){
			$owner[] = $state['state'][$i];
		}
			
		for($i = 3;$i <= 5;$i++){
			$guest[] = $state['state'][$i];
		}
		
		//$sum=array();
		for($i = 0;$i < 3;$i++){	
			
			$win[] = App_mora::getJudge($owner[$i], $guest[$i]);
			
			$num = $win;
		}

		$smarty->assign('num' , $win);
		$stake = $state['stake']/2;
		$stak = intval($stake*0.9)+$stake;
		//计算哪一方胜利
		$sum = array_sum($num);
		loadLib('Currency');
			
		//读取庄家信息
		$gains = App_mora::getGain($owner_uin , true);
		//读取闲家信息
		$gain = App_mora::getGain($uin , true);
		loadLib('User');
		$uin_info = User::getBaseInfo($owner_uin);
	/**************闲家赢*************/
		if($sum < 0){

			if($sum == -1){
				$g = $gains['gain']-25;
				$gs = $gain['gain']+25;
			}
			if($sum == -2){
				$g = $gains['gain']-30;
				$gs = $gain['gain']+30;
			}
			if($sum == -3){
				$g = $gains['gain']-40;
				$gs = $gain['gain']+40;
			}
			$total = $gains['total']+1;
			
			$gust_total = $gain['total']+1;
			
			$gust_win = $gain['win']+1;
			
			$win = $gains['win'];
			
			$rate = $win/$total*10000;
			$rate = intval($rate);
			$money = $stake;
			
			$wins = 3;
		
			$gust_rate = $gust_win/$gust_total*10000;
			$gust_rate = intval($gust_rate);
			Currency::update('app_21' , $uin , +$stak);

		}else if($sum == 0){
			/**************猜对三个算平局*************/
			if($num[0] == 0 && $num[1] == 0 && $num[2] == 0){
				/*********奖励5%筹码**********/
				$stake = intval($stake * 1.05);
				Currency::update('app_21' , $owner_uin , +$stake);
				Currency::update('app_21' , $uin , +$stake);
				$wins = 2;
				$money = $stake;	
			/**************庄家赢*************/
			}else if($num[0] === -1){
				$g = $gains['gain']+25;
				$gs = $gain['gain']-25;
				$total = $gains['total']+1;
			
				$gust_total = $gain['total']+1;
				
				$win = $gains['win']+1;
				
				$gust_win = $gain['win'];
				
				$rate = $win/$total*10000;
				$rate = intval($rate);
				$gust_rate = $gust_win/$gust_total*10000;
				$gust_rate = intval($gust_rate);
				$wins = 1;
				
				$money = $stak;
				
				Currency::update('app_21' , $owner_uin , +$stak);
				App_mora::updeteGain($owner_uin,$g,$total,$win,$rate);
			
				App_mora::updeteGain($uin,$gs,$gust_total,$gust_win,$gust_rate);
			}else{
				/**************庄家赢*************/
				if($num[0] === 0 && $num[1] === -1){
					$g = $gains['gain']+25;
					$gs = $gain['gain']-25;
					$total = $gains['total']+1;
				
					$gust_total = $gain['total']+1;
					
					$win = $gains['win']+1;
					
					$gust_win = $gain['win'];
					
					$rate = $win/$total*10000;
					$rate = intval($rate);
					$gust_rate = $gust_win/$gust_total*10000;
					$gust_rate = intval($gust_rate);
					$wins = 1;
					
					$money = $stak;
					
					Currency::update('app_21' , $owner_uin , +$stak);
					App_mora::updeteGain($owner_uin,$g,$total,$win,$rate);
			
					App_mora::updeteGain($uin,$gs,$gust_total,$gust_win,$gust_rate);
				}else{
				/**************闲家赢*************/
					$g = $gains['gain']-25;
					$gs = $gain['gain']+25;
					
					$total = $gains['total']+1;
			
					$gust_total = $gain['total']+1;
					
					$gust_win = $gain['win']+1;
					
					$win = $gains['win'];
					
					$rate = $win/$total*10000;
					$rate = intval($rate);
					$money = $stake;
					
					$wins = 3;
				
					$gust_rate = $gust_win/$gust_total*10000;
					$gust_rate = intval($gust_rate);
					Currency::update('app_21' , $uin , +$stak);
					App_mora::updeteGain($owner_uin,$g,$total,$win,$rate);
			
					App_mora::updeteGain($uin,$gs,$gust_total,$gust_win,$gust_rate);
				}
			}
	/**************庄家赢*************/
		}else{
			if($sum == 1){
				$g = $gains['gain']+25;
				$gs = $gain['gain']-25;
			}
			if($sum == 2){
				$g = $gains['gain']+30;
				$gs = $gain['gain']-30;
			}
			if($sum == 3){
				$g = $gains['gain']+40;
				$gs = $gain['gain']-40;
			}
			$total = $gains['total']+1;
			
			$gust_total = $gain['total']+1;
			
			$win = $gains['win']+1;
			
			$gust_win = $gain['win'];
			
			$rate = $win/$total*10000;
			$rate = intval($rate);
			$gust_rate = $gust_win/$gust_total*10000;
			$gust_rate = intval($gust_rate);
			$wins = 1;
			
			$money = $stak;
			
			Currency::update('app_21' , $owner_uin , +$stak);
		}
		loadLib('SysMsg');
			SysMsg::sendSystem($owner_uin , array(
				'owner' => $owner,
				'guest' => $guest,
				'send_uin' => $uin,
				'win' => $wins,
				'earn' => $money,
			) , 1005);
		if($sum != 0){
			App_mora::updeteGain($owner_uin,$g,$total,$win,$rate);
			
			App_mora::updeteGain($uin,$gs,$gust_total,$gust_win,$gust_rate);
		}
		//App_mora::addsteke($id,50);
		$smarty->assign('owner' , $owner);
		$smarty->assign('guest' , $guest);
		$smarty->assign('stake' , $stake);
		
		App_mora::delState($owner_uin , $id);
		
		$smarty->assign('uin_info' , $uin_info);
		
		$smarty->assign('wins' , $wins);
		$smarty->assign('id' , $id);
		return $smarty->fetch('app/mora/endguest.tpl.php');
	}
	function delete_mora(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
		$stake = isset($_POST['stake']) ? intval($_POST['stake']) : 0;
		if(empty($id)){
			return View::error('参数错误!');
		}
		$uin = SessionInfo('uin');
		$stake = $stake*0.9;
		loadLib('App_mora');
		$code = App_mora::delState($uin, $id);
		if($code === 0){
			loadLib('Currency');
			Currency::update('app_21' , $uin , +$stake);			
			header('Location: ' . SITEURL . '/app.php?m=mora&sid=' . sessionInfo('skey') );
		}else{
			return View::error('系统繁忙，请稍后再试' , $code);
		}
	}
	/**
	 * 猜拳玩法
	 */
	function morastate(){
		$smarty = loadSmarty();
		
		return $smarty->fetch('app/mora/state.tpl.php');
	}
	/**
	 * 猜拳道具
	 */
	function moraprop(){
		$smarty = loadSmarty();
		loadLib('App_mora');
		$uin = sessionInfo('uin');
		// 获取自己的战绩
		$gains = App_mora::getGain($uin , true);
		//获取梭哈道具
		loadLib('App_prop');
		$prop = App_prop::getAllProp('app_mora_prop');
		
		$smarty->assign('list' , $prop);
		$oneselfprop = App_prop::getAllOneselfProp('app_mora_oneself_prop', 'app_mora_prop', $uin);
	//print_r($oneselfprop);
		// 获取盈利
		$rank = App_mora::getRank($uin , $gains['gain']);
		/*******************查询金库******************/
		loadLib('Money');
		$money = Money::get($uin);
		if (empty($money)){
			return View::error('你的金库不存在!!!');
		}
		
		$smarty->assign('oneselfprop' , $oneselfprop);
		$smarty->assign('money',$money);
		$smarty->assign('rank' , $rank['name']);
		
		return $smarty->fetch('app/mora/prop.tpl.php');
	}
	/**
	 * 购买道具
	 */
	function addProp(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$photoscope = isset($_POST['photoscope']) ? intval($_POST['photoscope']) : null;
		if (empty($id) || empty($photoscope)){
			return View::error('参数错误!!!');
		}
		
		loadLib('App_prop');
		$list = App_prop::getOneProp('app_mora_prop', $id);
		if (empty($list)){
			return View::error('该道具不存在!!!');
		}
		$uin=sessionInfo('uin');
		/*******************查询金库******************/
		loadLib('Money');
		$money = Money::get($uin);
		if (empty($money)){
			return View::error('你的金库不存在!!!');
		}
		$gold = $photoscope * $list['price'];
		
		if ($gold > $money['gold']){
			return View::error('你的金币不足'.$gold.',请及时充值!!!');
		}
		/**********************查询自己的道具************************/
		$prop = App_prop::getOneselfProp('app_mora_oneself_prop', $uin, $id);
		/**********************记录为空时写入************************/
		if (empty($prop)){
			App_prop::getAddOneselfProp('app_mora_oneself_prop', $uin, $id, $photoscope);
		}else{
			//$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
			$count = $prop['count'] + $photoscope;
			App_prop::getAlterOneselfProp('app_mora_oneself_prop', $id, $uin, $count, $prop['time']);
		}
		/***********************扣除金币********************/
		$code = Money::update($uin, -$gold);
		if($code === 0){
			$money = $money['gold'] - $gold;
			//消费记录
			loadLib('Operate');
			Operate::getAddConsume($uin, 'app_mora_prop', $id, $gold);
			return View::success('你购买了' . $photoscope .'个'.$list['name'].',你的余额为' . $money . '金币');
		}else{
			return View::error('购买失败');
		}
	}
	/**
	 * 使用道具
	 */
	function scene(){
		$owner_uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$prop_id = isset($_GET['prop']) ? intval($_GET['prop']) : null;
		$uin = sessionInfo('uin');
		$smarty = loadSmarty();
		loadLib('App_mora');
		
		$state = App_mora::getState($owner_uin , $id);
		if (empty($state)){
			return View::error('该局不存在!!!');
		}
		
		loadLib('App_prop');
		$list = App_prop::getOneProp('app_mora_prop', $prop_id);
		if (empty($list)){
			return View::error('该道具不存在!!!');
		}
		
		/**********************查询自己的道具************************/
		$prop = App_prop::getOneselfProp('app_mora_oneself_prop', $uin, $prop_id);
		if (empty($prop)){
			return View::error('你没有此道具!!!' , 0 ,array(
				array('name' => '前往商店' , 'url' => '/app.php?m=showhand&amp;a=showhandproperty&amp;sid=' . sessionInfo('skey')),
				));
		}
		for ($i = 0 ; $i < 3 ; $i++){
			$is_state[] = $state['state'][$i];
			
		}
		$state['state'] = '';
		$state['state'] = $is_state;
	
		$code=App_mora::getUpMorastate($owner_uin , $id , $state['state']);
		//print_r($list['type']);
		/*****************重猜*****************/
		if ($list['type'] == 'repeat') {
			
			//防止刷新
			header('Location: ' . SITEURL . '/app.php?m=mora&a=chall&uin=' . sessionInfo('uin') . '&owner_uin=' . $owner_uin . '&id=' . $id . '&sid=' . sessionInfo('skey') );	
		}
		if ($code == 0){
			$count = $prop['count'] -1;
			if ($count <= 0){
				App_prop::delProp('app_mora_oneself_prop', $uin, $prop_id);
			}else{
				App_prop::getAlterOneselfProp('app_mora_oneself_prop', $prop_id, $uin, $count, $prop['time']);
			}
		}else{
				return View::error('网络繁忙,请稍后再试！');
		}
		
	}
}
# end of script